package life.fairy.community.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class PaginationDTO<T> {
    private List<T> data;
    private boolean showPrevious;
    private boolean showNext;
    private boolean showEndPage;
    private boolean showFirstPage;
    private Integer page;
    private List<Integer> pages = new ArrayList<>();
    private Integer totalPage;

    public void setPagination(Integer totalCount, Integer page, Integer size) {

        /*
        * 通过totalCount、page、size对页面展示的分页情况进行统计和设置，保证分页组件中内容正常显示
         */

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }

        if (totalCount == 0) {
            showFirstPage = false;
            showPrevious = false;
            showNext = false;
            showEndPage = false;
        }

        if (page < 1) {
            page = 1;
        }

        if (page > totalPage) {
            page = totalPage;
        }

        this.page = page;

        pages.add(page);
        for(int i=1; i<=3; i++) {
            if(page-i > 0) {
                pages.add(0, page-i);
            }

            if (page + i <= totalPage) {
                pages.add(page+i);
            }
        }

        // 是否展示上一页
        if (page == 1) {
            showPrevious = false;
        } else {
            showPrevious = true;
        }
        // 是否展示下一页
        if (page == totalPage) {
            showNext = false;
        } else {
            showNext = true;
        }
        // 是否展示第一页跳转
        if (pages.contains(1)) {
            showFirstPage = false;
        }else {
            showFirstPage = true;
        }
        // 是否展示最后一页跳转
        if (pages.contains(totalPage)) {
            showEndPage = false;
        }else {
            showEndPage = true;
        }

    }
}
